package com.xzw.cameratest.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.business.entity.ChannelInfo;
import com.android.business.entity.GroupInfo;
import com.android.business.exception.BusinessException;
import com.xzw.cameratest.R;
import com.xzw.cameratest.activty.PlayOnlineActivity;
import com.xzw.cameratest.adapter.OrganizationTreeAdapter;
import com.xzw.cameratest.async.AsyncBuilder;
import com.xzw.cameratest.base.BaseFragment;
import com.xzw.cameratest.bean.Node;
import com.xzw.cameratest.organization.GroupLoadManager;

import java.util.ArrayList;
import java.util.List;

public class OrganizationTreeFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private OrganizationTreeAdapter adapter;

    private List<Node> mVisibleNodes = new ArrayList<>();

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_organization_tree, null);
        initView(view);
        return view;
    }

    private void initView(View view){
        adapter = new OrganizationTreeAdapter(getActivity(),mVisibleNodes);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        loadGroup();
    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new OrganizationTreeAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Node node = mVisibleNodes.get(position);
                if (node.getData() instanceof ChannelInfo){
                    onClickChannelNode((ChannelInfo) node.getData());
                }else if (node.getData() instanceof GroupInfo){
                    expandOrCollapse(position);
                }
            }
        });
    }

    private void refreshVisibleNode(){
        mVisibleNodes.clear();
        mVisibleNodes.addAll(filterVisibleNode());
        adapter.notifyDataSetChanged();
    }

    private List<Node> filterVisibleNode(){
        List<Node> result = new ArrayList<>();
        for (Node node : GroupLoadManager.getInstance().getAllNodes()) {
            if (node.isRoot() || node.isParentExpand()){
                result.add(node);
            }
        }
        return result;
    }

    private void expandOrCollapse(int position){
        Node node = mVisibleNodes.get(position);
        node.setExpand(!node.isExpand());
        if (!node.isLoadChannel()){
            GroupInfo groupInfo = (GroupInfo) node.getData();
            loadChannel(node,groupInfo.getChannelList());
        }else{
            refreshVisibleNode();
        }
    }

    public void onClickChannelNode(ChannelInfo channelInfo){
        PlayOnlineActivity.open(getActivity(), channelInfo);
        showToast(channelInfo.getName());
    }


    private void loadGroup() {
        AsyncBuilder.createTask(new AsyncBuilder.BusinessNullRetTask() {
            @Override
            public void doInBackground() throws Exception {
                GroupLoadManager.getInstance().loadGroup();
            }
        }).loading(new AsyncBuilder.LoadingListener() {
            @Override
            public void beginLoading() {
                showProgressDialog();
            }

            @Override
            public void finishLoading() {
                dismissProgressDialog();
            }
        }).resultInMain(this, new AsyncBuilder.ResultNullListener() {
            @Override
            public void onSuccess() {
                refreshVisibleNode();
            }

            @Override
            public void onError(BusinessException e) {
                showToast(getString(R.string.organization_tree_organization_load_fail));
            }
        });
    }

    private void loadChannel(final Node parentNode , final List<String> channelIdList){
        AsyncBuilder.createTask(new AsyncBuilder.BusinessNullRetTask() {
            @Override
            public void doInBackground() throws Exception {
                GroupLoadManager.getInstance().loadChannel(parentNode,channelIdList);
            }
        }).loading(new AsyncBuilder.LoadingListener() {
            @Override
            public void beginLoading() {
                showProgressDialog();
            }

            @Override
            public void finishLoading() {
                dismissProgressDialog();
            }
        }).resultInMain(this, new AsyncBuilder.ResultNullListener() {
            @Override
            public void onSuccess() {
                refreshVisibleNode();
            }

            @Override
            public void onError(BusinessException e) {
                refreshVisibleNode();
            }
        });
    }


}
